package com.jianan.mis.app.menu;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.jianan.mis.app.AppPerspective;
import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.ImageManager;

public class ShowMenuAction extends Action {

	public final static String ID = "com.jianan.mis.app.menu.showMenu";

	private IWorkbenchWindow window;

	public ShowMenuAction(IWorkbenchWindow window) {
		this.window = window;
		setId(ID);
		setActionDefinitionId(ID);
		setText("显示菜单(&M)");
		setToolTipText("显示菜单视图");
		setImageDescriptor(ImageManager.getImageDescriptor(IImageKeys.MODULES));
	}

	public void run() {
		try {
			window.getActivePage().setPerspective(
					PlatformUI.getWorkbench().getPerspectiveRegistry()
							.findPerspectiveWithId(
									AppPerspective.PERSPECTIVE_ID));
			window.getActivePage().showView(MenuView.ID);
		} catch (PartInitException ex) {
			ex.printStackTrace();
			MessageDialog.openError(
					window.getShell().getDisplay().getActiveShell(), 
					IMessageKeys.OPEN_VIEW_FAIL, ex.getMessage());
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}
}
